// 主界面
;define(function(require, exports, module) {
//  'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var wareHouseDetail = require("./wareHouseDetail.html");
    var databind = require("databind");
    var tools = require("tools");
    var num = 0;
    var lock = false;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(num){
    	footer.init();  	
    	$("#footer").removeClass("none");

    	leftMenu.init(num);
    	
    	$(".memberGradeAduitList").parent().parent().parent().addClass("leftMenu-open");
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".distributors").addClass("leftMenu-active");
    	
    	topMenu.init(1);
    	$(".footer").removeClass("none");
    	
    	tools.loadList("inquireWarehouseList.json", $(".leftDiv ul"), $(".leftDiv script"), {}, function(){
    		$(".leftDiv ul li").unbind("click").click(function(){
    			var $this = $(this);
    			showWarehouseMapForFinance($this.attr("value"));
    		});
    	}, null);
		initEvent();
		router.init();
    };
    
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		
    };
    
    //查询仓库详情
	var showWarehouseMapForFinance = function(warehouseId) {
		base.fnAjax({
			action: "showWarehouseMapForFinance.json",
			oData: {
				warehouseId: warehouseId
			}, //传递参数
			nocode: true,
			fnSuccess: function(data) {
				if (data.status == 200) {
//					$(".mainDiv img").attr("src", data.datas[0].warehouse.map_img);
					layer.open({
					  type: 1,
					  title: '详情',
					  shadeClose: true,
					  shade: 0.8,
					  shadeClose: true,
					  maxmin: true,
					  area: ['1200px', '800px'],
					  content: wareHouseDetail
					}); 
					$(".wareHouseDetail img").attr("src", data.datas[0].warehouse.map_img);
//					"<img src='"+data.datas[0].warehouse.map_img+"' />"
					$.each(data.datas[0].checkViews, function() {
						createBox(this);
					});
					
					// 创建拖拽方法
					$("#canvas").mousedown(function(e) {
						var canvas = $(this);
						e.preventDefault();
						var pos = $(this).position();
						this.posix = {
							'x' : e.pageX - pos.left,
							'y' : e.pageY - pos.top
						};
						$.extend(document, {
							'move' : true,
							'move_target' : this,
							'call_down' : function(e, posix) {
								canvas.css({
									'cursor' : 'move',
									'top' : e.pageY - posix.y,
									'left' : e.pageX - posix.x
								});
							},
							'call_up' : function() {
								canvas.css('cursor', 'default');
							}
						});
					}).on('mousedown', '.box', function(e) {
						if (lock)
							return;
						var pos = $(this).position();
						this.posix = {
							'x' : e.pageX - pos.left,
							'y' : e.pageY - pos.top
						};
						$.extend(document, {
							'move' : true,
							'move_target' : this
						});
						e.stopPropagation();
					}).on('mousedown', '.box .coor', function(e) {
						var $box = $(this).parent();
						var posix = {
							'w' : $box.width(),
							'h' : $box.height(),
							'x' : e.pageX,
							'y' : e.pageY
						};
						$.extend(document, {
							'move' : true,
							'call_down' : function(e) {
								$box.css({
									'width' : Math.max(30, e.pageX - posix.x + posix.w),
									'height' : Math.max(30, e.pageY - posix.y + posix.h)
								});
							}
						});
						e.stopPropagation();
					});

				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	var createBox = function(data) {
		var dataId = data.id || '';
		var value = data.map_area_text || '';
		var color = data.map_area_color || '';
		var height = data.map_area_height || 0;
		var width = data.map_area_width || 0;
		var pageX = data.map_area_page_x || 300;
		var pageY = data.map_area_page_y || 100;
		var locationCode = data.check_code;

		// 更新计数器并记录当前计数
		var curNum = num++;
		// 创建区域块
		var pos = $("#canvas").position();
		var box;
		if (value.indexOf("叉车") == -1) {
			box = $('<div class="box" rel="'
							+ curNum
							+ '" dataId="'
							+ dataId
							+ '"><pre class="content">'
							+ value
							+ '</pre><div class="bg transparent" style="background-color:'
							+ color
							+ '"></div><div class="coor transparent"></div> </div>').css({
								width : width,
								height : height,
								top : pageY > 0 ? pageY : (pos.top > 0 ? 0
										: pos.top * -1 + 50),
								left : pageX > 0 ? pageX : (pos.left > 0 ? 0
										: pos.left * -1 + 30)
							}).appendTo("#canvas");
		} else {
			box = $(
					'<div class="box" rel="'
							+ curNum
							+ '" dataId="'
							+ dataId
							+ '"><pre class="content">'
							+ value
							+ '</pre><div class="bg transparent" style="background-color:'
							+ color
							+ '"></div><div class="coor transparent"></div><div style="position: absolute;"><img src="../locationMapImg/truck.png"/> </div>  </div>')
					.css(
							{
								width : width,
								height : height,
								top : pageY > 0 ? pageY : (pos.top > 0 ? 0
										: pos.top * -1 + 50),
								left : pageX > 0 ? pageX : (pos.left > 0 ? 0
										: pos.left * -1 + 30)
							}).appendTo("#canvas");
		}

		// 计算文本位置
		box.find('.content').css({
			marginLeft : box.find('.content').width() / 2 * -1,
			marginTop : box.find('.content').height() / 2 * -1
		});

		var a = value.indexOf("监控");
		if (a >= 0) {
			var name = '.box[rel=' + curNum + ']';
			$(name).unbind("click").click(function(){
				// iframe层-多媒体
				layer.open({
					type : 2,
					title : false,
					area : [ '90%', '90%' ],
					shade : 0.8,
					closeBtn : 1,
					shadeClose : true,
					content : 'http://120.24.80.87:8080/warehouse/checkview/showList?monitorId=' + dataId
				});// ===end
			});
//			$(name).live('click', function() {
//				// iframe层-多媒体
//				layer.open({
//					type : 2,
//					title : false,
//					area : [ '90%', '90%' ],
//					shade : 0.8,
//					closeBtn : 1,
//					shadeClose : true,
//					content : '../checkview/showList?monitorId=' + dataId
//				});// ===end
//			});

		} 
//		else {
//			context.attach('.box[rel=' + curNum + ']', [ {
//				text : '货品明细查看',
//				action : function(e) {
//					e.preventDefault();
//					// 弹出即全屏
//					var index = layer.open({
//						type : 2,
//						content : '../storage/stock_detailByLocation?location='
//								+ $('.box[rel=' + curNum + '] .content').text()
//								+ '&warehouseId=' + $("#warehouseId").val(),
//						area : [ '724px', '468px' ],
//						maxmin : true
//					});
//				}
//			} ]);
//		}
	}
    
    return {
        init:init
    };
});